import json

from cross_chain_data.code_new.code_scan.bscScan import get_bscScan_data
from cross_chain_data.code_new.code_scan.bnbScan import get_bnbScan_data
from cross_chain_data.code_new.code_scan.etherScan import get_ethScan_data
from cross_chain_data.code_new.code_scan.btcScan import get_btcScan_data
from cross_chain_data.code_new.code_scan.ltcScan import get_ltcScan_data
from cross_chain_data.code_new.code_scan.avaxScan import get_avaxScan_data
from cross_chain_data.code_new.code_scan.dogeScan import get_dogeScan_data
from cross_chain_data.code_new.code_scan.bchScan import get_bchScan_data
from cross_chain_data.code_new.code_scan.gaiaScan import get_gaiaScan_data
from cross_chain_data.code_new.ThorChain import get_thorchain_data

# 1.从跨链平台thorchain抓取数据,并记录下来
thorchain_data=get_thorchain_data()
# f=open('../data/data.json','r')
# content=f.read()
# f.close()
# thorchain_data=json.loads(content)
#
# print(type(thorchain_data))
#
# print(len(thorchain_data))
# 2.使用字典进行动态的选择调用的函数
chain_function_dict={
    'AVAX':get_avaxScan_data,
    'BTC':get_btcScan_data,
    'LTC':get_ltcScan_data,
    'ETH':get_ethScan_data,
    'BNB':get_bnbScan_data,
    'BCH':get_bchScan_data,
    'BSC':get_bscScan_data,
    'DOGE':get_dogeScan_data,
    'GAIA':get_gaiaScan_data
}
# 3.设置最终数据的存放列表
total_data=[]
# 统计链名
chain_name=[]
# 2.遍历跨链平台数据
print("************单链平台数据开始爬取************")
for dict_data_thorchain in thorchain_data:
    # 设置临时存放数据的字典
    tem_dict = {}
    # 获取源链目标链的链名
    input_chain=dict_data_thorchain["input"]["chain"]
    output_chain=dict_data_thorchain["output"]["chain"]

    if input_chain not in chain_name:
        chain_name.append(input_chain)
    if output_chain not in chain_name:
        chain_name.append(output_chain)

    print(f"源链:{input_chain}，目标链:{output_chain}")
    if input_chain in chain_function_dict and output_chain in chain_function_dict :
        # 先获取源链交易哈希
        source_hash=dict_data_thorchain["hash"]
        # 调用单链爬取数据函数
        dict_data_source=chain_function_dict[input_chain](source_hash)
        # 再获取目标链交易哈希
        target_hash=dict_data_thorchain["outbounds"][1]["hash"]
        # 调用单链爬取数据函数
        dict_data_target= chain_function_dict[output_chain](target_hash)
        # 将数据存放到临时字典中
        tem_dict["source_chain_data"]=dict_data_source
        tem_dict["cross_chain_data"]=dict_data_thorchain
        tem_dict["target_chain_data"]=dict_data_target
        # 将完整的一条数据存放到最终的列表中
        total_data.append(tem_dict)
    else:print("源链或者目标链的平台数据目前无法获取！")
    print("******************************")

# 将最终的数据写入文件中
print(f"全部数据爬取完成，最终总共{len(total_data)}条数据")
json_data=json.dumps(total_data,indent=4)
with open('../data_new/total_data.json', 'w', encoding='utf-8') as f:
    f.write(json_data)

# #抽取交易序列数据
# # 遍历总的数据
# print("******************交易序列数据开始抽取******************")
# # 使用字典进行动态的选择调用的函数
# chain_function_xulie_source={
#     'AVAX':get_avaxScan_data,
#     'BTC':get_btcScan_data,
#     'LTC':get_ltcScan_data,
#     'ETH':get_cctx_xulie_event_eth_bsc_source,
#     'BNB':get_bnbScan_data,
#     'BCH':get_bchScan_data,
#     'BSC':get_cctx_xulie_event_eth_bsc_source,
#     'DOGE':get_dogeScan_data,
#     'GAIA':get_gaiaScan_data
# }
# chain_function_xulie_target={
#     'AVAX':get_avaxScan_data,
#     'BTC':get_btcScan_data,
#     'LTC':get_ltcScan_data,
#     'ETH':get_cctx_xulie_event_eth_bsc_target,
#     'BNB':get_bnbScan_data,
#     'BCH':get_bchScan_data,
#     'BSC':get_cctx_xulie_event_eth_bsc_target,
#     'DOGE':get_dogeScan_data,
#     'GAIA':get_gaiaScan_data
# }
# # 抽取的序列数据
# data_xulie_cctx=[]
# for data_cctx_dict in total_data:
#     # 设置单条字典数据
#     data_xulie={}
#     # 获取源链目标链的名字
#     source_chain_name = data_cctx_dict["cross_chain_data"]["input"]["chain"]
#     target_chain_name = data_cctx_dict["cross_chain_data"]["output"]["chain"]
#
#     if source_chain_name in chain_function_xulie_source and target_chain_name in chain_function_xulie_source:
#         # 1.抽取源链事件
#         Lock_event,Deposit_event=chain_function_xulie_source[source_chain_name](data_cctx_dict)
#         # 2.抽取跨链平台事件
#         Lock_action, Unlock_action=get_cross_chain_event(data_cctx_dict)
#         # 3.抽取目标链事件
#         Unlock_event=chain_function_xulie_target[target_chain_name](data_cctx_dict)
#
#         # 4.添加数据到空字典中，并打标签
#         data_xulie["Lock_event"]=Lock_event
#         data_xulie["Deposit_event"]=Deposit_event
#         data_xulie["Lock_action"]=Lock_action
#         data_xulie["Unlock_action"]=Unlock_action
#         data_xulie["Unlock_event"]=Unlock_event
#         data_xulie["Type"]="1"
#
#         data_xulie_cctx.append(data_xulie)
#     else:
#         print("源链或者目标链的平台数据目前无法获取！")
#
# # 将最终的数据写入文件中
# print(f"全部数据抽取完成，最终总共{len(data_xulie_cctx)}条数据")
# json_data=json.dumps(data_xulie_cctx,indent=4)
# with open('../data_new/total_data_xulie.json', 'w', encoding='utf-8') as f:
#     f.write(json_data)
# get_bnbScan_data("1545AD1800DBF7ABE5850F7489610602F77A51CC53CECB4619F9BDFBF4A49535")
# get_ethScan_data("85f7535e54d35b1af279ffcfae0160056ef0baa585c3f5c9941d66008d358051")
# get_btcScan_data("5C23ED5E73F53B3B62E05940B6C2F4B8067C20594F65470BC27503E76DFB375C")
# get_ltcScan_data("3e2d116fce94806f233eee48c89d7c395fcfba2eabaa6b233a4efce7d1f7f616")
# get_avaxScan_data("578D715DD72A1B7CC5DFC56E5E8D5D995BAA3E12149DB03BE3E35BC3139BE085")
# get_dogeScan_data("bcc4ce3f004b02b50456976dfc4de69d651ba718ce95422f51cd1d4bf7235abb")
# get_bchScan_data("06DDCC502589FC4C0F975F7DBA3F00728740AD40B09C5203187827DAFB2069AE")


